package com.ssyc.student.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ssyc.common.adapter.CommonAdapter;
import com.ssyc.common.adapter.ViewHolder;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.utils.ScreenUtils;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.student.R;
import com.ssyc.student.bean.StImgPrpareInfo;

import java.util.List;

/**
 * 学生端--单词预习界面--选择图片的列表适配器
 * Created by Administrator on 2018/5/11 0011.
 */

public class StGvWordPrepareAdapter extends CommonAdapter<StImgPrpareInfo> {

    private Context context;

    //正确答案
    private String rightAnswer;

    //条目宽高的布局参数
    private LinearLayout.LayoutParams lp;

    public StGvWordPrepareAdapter(String rightAnswer, Context context, List<StImgPrpareInfo> datas, int layoutId) {
        super(context, datas, layoutId);
        this.rightAnswer = rightAnswer;
        this.context = context;
        int width = (ScreenUtils.getScreenWidth(context) - UiUtils.dp2px(16 * 2 + 8)) / 2;
        int height = width * 123 / 173;
        lp = new LinearLayout.LayoutParams(width, height);
    }

    @Override
    public void convert(ViewHolder holder, StImgPrpareInfo stImgPrpareInfo) {
        //外层的布局
        RelativeLayout rlOutside = holder.getView(R.id.rl_outside);
        rlOutside.setLayoutParams(lp);

        //需要展示的图片
        ImageView iv = holder.getView(R.id.iv);
        ImageUtil.displayImage(context, iv, stImgPrpareInfo.url);

        //显示对错的遮罩层视图
        View viewState = holder.getView(R.id.view_state);
        //没选择前,遮罩层是隐藏状态
        if (stImgPrpareInfo.isChoosed) {
            viewState.setVisibility(View.VISIBLE);
        } else {
            viewState.setVisibility(View.GONE);
        }
        //设置遮罩层的背景图,需要通过跟正确答案或者位置进行比对后确定
        if (stImgPrpareInfo.url.endsWith("/" + rightAnswer)) {
            viewState.setBackgroundResource(R.drawable.st_choose_img_right);
        } else {
            viewState.setBackgroundResource(R.drawable.st_choose_img_error);
        }
    }
}
